<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="稽核员" prop="userName">
        <el-input v-model="queryParams.userName" placeholder="请输入稽核员" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="分配时间" style="width: 308px">
        <el-date-picker v-model="daterangeSetupTime" value-format="YYYY-MM-DD" type="daterange" range-separator="-" start-placeholder="开始日期"
          end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['audit:cAuditor:add']">新增</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <Custable :loading="loading" :content="config.goOutContent" :list="cAuditorList" :queryParams="queryParams" :total="total" @getList="getList">
      <template #operation="scope">
        <!-- <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['audit:stat_ycbq:edit']">修改</el-button> -->
        <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['audit:stat_ycbq:edit']">删除</el-button>
      </template>
    </Custable>

    <el-dialog title="新增" v-model="dialogVisible" width="800px" append-to-body>
      <div class="content">
        <el-transfer v-model="selectData" :data="transferData" :titles="['稽核人员', '集中稽核']" :props="{ key: 'userId', label: 'userName' }" @change="handleChange"
          :right-default-checked="selectData">
          <template #default="{ option }">
            <span>{{ option.userName }} - {{ option.deptName }}</span>
          </template>
        </el-transfer>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm" v-hasPermi="['audit:award:edit']">确 定</el-button>
          <el-button @click="handleClose">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    <!-- <Mask v-if="dialogVisible" :dialogVisible="dialogVisible" @handleClose="handleClose" :queryParams="queryParams" :rowDetails="rowDetails"
      :rules="config.rules" :title="title" :formItemConfig="config.formItemConfig" /> -->
  </div>
</template>

<script setup name="CAuditor">
import {
  listCAuditor,
  getCAuditor,
  delCAuditor,
  getCAuditorList,
  addCAuditor,
  updateCAuditor,
} from "@/api/charge/cAuditor";
import Custable from "@/components/Custom/Custable";
import config from "./config";
import Mask from "./components/Mask";
import { deptTreeSelect, getUser, listUser } from "@/api/system/user";

const { proxy } = getCurrentInstance();

const cAuditorList = ref([]);
const dialogVisible = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const total = ref(0);
const title = ref("");
const daterangeSetupTime = ref([]);
const rowDetails = ref({});
const postUserId = ref();
const deptUserId = ref();
const transferData = ref([]);
const selectData = ref([]);
const oldSelectData = ref([])

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    userName: null,
  },
  rules: {},
});

const { queryParams, form, rules } = toRefs(data);

/** 查询集中稽核人员名单列表 */
function getList() {
  loading.value = true;
  if (null != daterangeSetupTime && "" != daterangeSetupTime) {
    queryParams.value["startDate"] = daterangeSetupTime.value[0];
    queryParams.value["endDate"] = daterangeSetupTime.value[1];
  }
  listCAuditor(queryParams.value).then((response) => {
    cAuditorList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
const getTransferData = () => {
  getCAuditorList().then((response) => {
    transferData.value = response.data;
    loading.value = false;
  });
};

function getPostList() {
  listCAuditor({ pageNum: 1, pageSize: 100 }).then((response) => {
    if (response.code == 200 && response.rows && response.rows.length) {
      let arr = []
      response.rows.forEach(item => {
        arr.push(item.userId)
      })
      selectData.value = arr;
      oldSelectData.value = arr;
    }

  });
}
/** 提交按钮 */
function submitForm() {
  let arr = selectData.value.filter(item => !oldSelectData.value.some(toRemoveItem => toRemoveItem === item));
  addCAuditor(arr).then((response) => {
    proxy.$modal.msgSuccess("新增成功");
    open.value = false;
    getList();
  });
}
// 取消按钮
function handleClose() {
  dialogVisible.value = false;
  rowDetails.value = {};
  reset();
  getList();
}

// 表单重置
function reset() {
  form.value = {};
  proxy.resetForm("cAuditorRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  daterangeSetupTime.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 新增按钮操作 */
function handleAdd() {
  reset();

  dialogVisible.value = true;
  title.value = "添加集中稽核人员名单";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.userId || ids.value;
  getCAuditor(_id).then((response) => {
    form.value = response.data;
    dialogVisible.value = true;
    title.value = "修改集中稽核人员名单";
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.userId || ids.value;
  proxy.$modal
    .confirm('是否确认删除集中稽核人员名单编号为"' + _ids + '"的数据项？')
    .then(function () {
      return delCAuditor(_ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "audit/cAuditor/export",
    {
      ...queryParams.value,
    },
    `cAuditor_${new Date().getTime()}.xlsx`
  );
}

getList();
getPostList();
getTransferData();
</script>
